package com.qfjy.project.weixin.api.tuling;

import com.qfjy.project.weixin.api.tuling.bean.InputText;
import com.qfjy.project.weixin.api.tuling.bean.Perception;
import com.qfjy.project.weixin.api.tuling.bean.TulingBean;
import com.qfjy.project.weixin.api.tuling.bean.UserInfo;
import com.qfjy.project.weixin.util.WeixinUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

/**
 * @Classname TulingUtil
 * @Author guoweixin
 * @Description TODO  图灵 聊天机器人
 * @Date 2021/6/24 14:33
 * @Created by Administrator
 */
@Component
public class TulingUtil {
    /**
     * 注册图灵平台 http://www.tuling123.com/
     * 并完成实名认证。申请机器人。
     *  API教程接入文档：https://www.kancloud.cn/turing/www-tuling123-com/718227
     */

    private static String TULING_URL_POST="http://openapi.tuling123.com/openapi/api/v2";


    public String getResult(String msg){
        //1对象 转 JSON
        TulingBean tulingBean=new TulingBean();
        tulingBean.setReqType(0);

        Perception perception=new Perception();
        InputText inputText=new InputText();
        inputText.setText(msg);
        perception.setInputText(inputText);
        tulingBean.setPerception(perception);

        UserInfo userInfo=new UserInfo();
        userInfo.setUserId("java2101");
        userInfo.setApiKey("acc513be8b5e4b26929247e83132f116");
        tulingBean.setUserInfo(userInfo);

        JSONObject json1=JSONObject.fromObject(tulingBean);

        System.out.println(json1.toString());
        //2 向服务器接口地址 发送POST请求
        JSONObject jsonObject= WeixinUtil.httpRequest(TULING_URL_POST,"POST",json1.toString());
        //3得到图灵服务器机器人返回的结果

        JSONArray  json2= (JSONArray) jsonObject.get("results");
        JSONObject json3= (JSONObject) json2.get(0);
        JSONObject json4= (JSONObject) json3.get("values");
        String result= json4.getString("text");

        return result;

    }










    public static void main(String[] args) {

     //1对象 转 JSON
        TulingBean tulingBean=new TulingBean();
        tulingBean.setReqType(0);

        Perception perception=new Perception();
        InputText inputText=new InputText();
        inputText.setText("吃饭了吗");
        perception.setInputText(inputText);
        tulingBean.setPerception(perception);

        UserInfo userInfo=new UserInfo();
        userInfo.setUserId("java2101");
        userInfo.setApiKey("acc513be8b5e4b26929247e83132f116");
        tulingBean.setUserInfo(userInfo);

        JSONObject json1=JSONObject.fromObject(tulingBean);

        System.out.println(json1.toString());
      //2 向服务器接口地址 发送POST请求
    JSONObject jsonObject= WeixinUtil.httpRequest(TULING_URL_POST,"POST",json1.toString());
    //3得到图灵服务器机器人返回的结果

       JSONArray  json2= (JSONArray) jsonObject.get("results");
       JSONObject json3= (JSONObject) json2.get(0);
       JSONObject json4= (JSONObject) json3.get("values");
       String result= json4.getString("text");
        System.out.println(result);
    }

}
